/*
 * @Author: duanguang
 * @Date: 2020-06-04 15:37:47
 * @LastEditTime: 2022-04-08 13:54:06
 * @LastEditors: duanguang
 * @Description: 
 * @FilePath: /legions-server/app/controller/repository_project.ts
 * 「扫去窗上的尘埃，才可以看到窗外的美景。」
 */
import { roleAuthority } from '@/abstract/controller/decorators';
import { UserRoleEnum } from "@/constants/enum.types";
import ResponseModel,{ PageResponseModel } from '@/model/responseModel';
import CommonController from '@/abstract/controller/common';
export default class RepositoryProjectController extends CommonController {
  @roleAuthority(UserRoleEnum.developer)
  public async list() {
    const query = this.ctx.query as any;
    try {
      const res = await this.ctx.service.repositoryProject.getList({
        ...query,
        userRule: this.userInfo?.roleNo,
        userId: this.userInfo?.uuid,
      })
      this.ctx.body = new PageResponseModel({
        success: true,
        message: '查询成功',
        data: res,
      });
    } catch (error) {
      this.ctx.body = new PageResponseModel({
        success: false,
        // @ts-ignore
        message: error && error.toString(),
      });
    }
  }
  @roleAuthority(UserRoleEnum.developer)
  public async update() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate(
      {
        project_name: { type: 'string',required: true },
        project_description: { type: 'string',required: true },
        repository_source: { type: 'string',required: true },
        project_type: { type: 'string',required: true },
        repository_https: { type: 'string',required: true },
        repository_ssh: { type: 'string',required: false },
        message_notify: { type: 'string',required: true },
        message_channel: { type: 'string',required: false },
      },
      body,
    )
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {

      const res = await this.ctx.service.repositoryProject.updateProject({
        ...body,
        creator_id: this.userInfo?.uuid,
      });
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: '修改成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error && error.toString(),
      });
    }
  }
  /**
   * 添加项目
   *
   * @memberof RepositoryProjectController
   */
  @roleAuthority(UserRoleEnum.developer)
  public async add() {
    const { ctx } = this;
    const { body } = ctx.request;
    const errors = this.app.validator.validate(
      {
        project_name: { type: 'string',required: true },
        project_description: { type: 'string',required: true },
        repository_source: { type: 'string',required: true },
        project_group: { type: 'string',required: true },
        repository_https: { type: 'string',required: true },
        repository_ssh: { type: 'string',required: false },
        message_notify: { type: 'string',required: true },
        message_channel: { type: 'string',required: false },
      },
      body,
    )
    if (errors) {
      this.ctx.body = new ResponseModel({
        success: false,
        data: errors,
        message: '参数错误',
      });
      return;
    }
    try {

      const res = await this.ctx.service.repositoryProject.createProject({
        ...body,
        creator_id: this.userInfo?.uuid,
        creator: this.userInfo?.name
      });
      this.ctx.body = new ResponseModel({
        success: true,
        data: res,
        message: '新增成功',
      });
    } catch (error) {
      this.ctx.body = new ResponseModel({
        success: false,
        // @ts-ignore
        message: error && error.toString(),
      });
    }
  }
}
